package com.ljs.tranctionldemo.service.impl;

import com.github.pagehelper.Page;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.ljs.tranctionldemo.bean.Users;
import com.ljs.tranctionldemo.dao.UserDao;
import com.ljs.tranctionldemo.request.MoneyToinRequest;
import com.ljs.tranctionldemo.response.MoneyResponse;
import com.ljs.tranctionldemo.service.UserService;
import com.ljs.tranctionldemo.util.BasePageResult;
import com.ljs.tranctionldemo.util.Result;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import javax.annotation.Resource;
import java.math.BigDecimal;
import java.util.List;

/**
 * @Description:
 * @Author: liujinsheng
 * @Date: 2021/6/28.
 */
@Service
public class UserServiceImpl implements UserService {
    @Resource
    UserDao userDao;
 @Transactional(rollbackFor = Exception.class)
    @Override
    public Boolean add(Users users) {
        int i=2;
        if(true){
            i= i/0;
        }
        return userDao.add(users);
    }

    @Override
    public Boolean delete(String id) {
        return userDao.delete(id);
    }
    //加上事务
    @Transactional(rollbackFor = Exception.class)
    @Override
    public Boolean update(Users users) {
        //获取自身原本的money
        Users users1=  userDao.findById(users.getId());
        float money=users1.getMoney();


        System.out.println(money);
        BigDecimal bigDecimal =new BigDecimal(money);
        Double addMoney=10.25;
        BigDecimal bigDecimal1=new BigDecimal(addMoney);

        //原来的价格小于15
        if(money<15){
            BigDecimal bigDecimal2=bigDecimal.add(bigDecimal1);
            float addvalue= bigDecimal2.floatValue();
            System.out.println("计算后的结果"+addvalue);
            users.setMoney(addvalue);

        }
        System.out.println("users"+users);
        return userDao.update(users);
    }

    @Override
    public Result<BasePageResult<List<Users>>> findAll(int page,int size) {
        System.out.println(page+","+size);
        //分页工具
        PageHelper.startPage(page,size);
        List<Users> usersList=   userDao.findAll(page,size);
        PageInfo pageInfo=new PageInfo(usersList);
       Long count=   pageInfo.getTotal();
        System.out.println("count"+count);
      int pages=     pageInfo.getPages();
         int pagesize=      pageInfo.getPageSize();
        System.out.println("pages"+pages);
        System.out.println("pagesize"+pagesize);
         int pagenum=  pageInfo.getPageNum();
        System.out.println("pagenum"+pagenum);

        BasePageResult basePageResult=new BasePageResult();
        basePageResult.setRecords(usersList);
        basePageResult.setTotal(count);
        basePageResult.setPage((long) page);
        basePageResult.setSize((long) size);
        System.out.println(pageInfo);
        return Result.ofSuccess(basePageResult) ;
    }

    @Override
    public Users findById(String id) {
        return userDao.findById(id);
    }

    @Override
    public Result<MoneyResponse> transTo(MoneyToinRequest moneyToinRequest) {
        MoneyResponse moneyResponse=new MoneyResponse();
       float finalMoney=0;

       Users users=  userDao.findById(moneyToinRequest.getId());
        if(users==null){
            return Result.ofFailMsg("不存在这个用户");
        }
       //当前用户的余额
        float money0=users.getMoney();
        float money=moneyToinRequest.getMoney();
             if(money0<=0){

                 return Result.ofFailMsg("余额不足");
             }else{
                 System.out.println("用户的余额"+money0);
                 System.out.println("要转出的金额"+money);
                 if(money>money0){
                     return Result.ofFailMsg("余额不足");
                 }else{
                     //余额
                 BigDecimal bigDecimal0=new BigDecimal(money0+"");
                 //要转出的值
                     BigDecimal bigDecimal=new BigDecimal(money+"");
                     BigDecimal bigDecimal1=     bigDecimal0.subtract(bigDecimal);
                     finalMoney=bigDecimal1.floatValue();
                     System.out.println("减去后的余额"+finalMoney);
                     moneyToinRequest.setMoney(finalMoney);
                     userDao.transTo(moneyToinRequest);
                     moneyResponse.setBln(true);
                 }
             }

        return Result.ofSuccess(moneyResponse);
    }

    @Override
    public Result<MoneyResponse> transIn(MoneyToinRequest moneyToinRequest) {
        float money=moneyToinRequest.getMoney();
        MoneyResponse moneyResponse=new MoneyResponse();
        float finalMoney=0;
        Users users=  userDao.findById(moneyToinRequest.getId());
        if(users==null){
            return Result.ofFailMsg("不存在这个用户");
        }
        //当前用户的余额
        float money0=users.getMoney();

                //余额
                BigDecimal bigDecimal0=new BigDecimal(money0+"");
                //要转出的值
                BigDecimal bigDecimal=new BigDecimal(money+"");
                BigDecimal bigDecimal1=     bigDecimal0.add(bigDecimal);
                finalMoney=bigDecimal1.floatValue();
        System.out.println("转入的后的余额"+finalMoney);
        moneyToinRequest.setMoney(finalMoney);

        userDao.transIn(moneyToinRequest);
        moneyResponse.setBln(true);
        return Result.ofSuccess(moneyResponse);
    }
    //加上事务，防止出问题
    @Transactional(rollbackFor = Exception.class)
    @Override
    public Result<MoneyResponse> trans(String toId, String inId, float money) {
        MoneyResponse moneyResponse=new MoneyResponse();
        MoneyToinRequest moneyToinRequest=new MoneyToinRequest();
        moneyToinRequest.setId(toId);
        moneyToinRequest.setMoney(money);
        //转出
        Result<MoneyResponse> responseResult= transTo(moneyToinRequest);
        MoneyResponse moneyResponse1=   responseResult.getData();

        if(moneyResponse1==null){
            return  Result.ofFailMsg(responseResult.getMsg());
        }
        //如果报错的话，没有事务，还是会去执行
        int i=0;
        i=i/0;
        //转入
        moneyToinRequest.setId(inId);
        moneyToinRequest.setMoney(money);
        Result<MoneyResponse> result=   transIn(moneyToinRequest);
        MoneyResponse moneyResponse2=  result.getData();
        if(moneyResponse2==null){
            return   Result.ofFailMsg(result.getMsg());
        }
        moneyResponse.setBln(true);
    return  Result.ofSuccess(moneyResponse);
    }
}
